<?php

class genericDB {

		// private attributes
		var $_field;
		var $_value;
		var $_newfield;
		var $_newvalue;
		var $_type;
		var $_pk;
		var $_table;
		var $_message;
		var $result;
		var $_debug;
				
		function genericDB($table) // constructor
		{	
			$this->_table = $table;
			$this->_field = array();
			$this->_value = array();
			$this->_type = array();
			$this->_newfield = array();
			$this->_debug = false;
			$result = mysql_query("SHOW FIELDS FROM ".$this->_table.";");
			if(mysql_num_rows($result)>0) 
			while($row = mysql_fetch_array($result))
			{									
				if($row["Key"]=="PRI") $this->_pk = $row["Field"]; else $this->_field[]= $row["Field"];
				$this->_type[$row["Field"]] = $row["Type"];
			}
		
		//print_r($this->_type); echo("<br>"); print_r($this->_field);
			$this->_value[$this->_pk] = '0';
			$this->_message = array(

								"msg_delete" => "El registro se eliminó.",
									"msg_update" => "El registro fue actualizado.",
									"msg_insert" => "El registro fue insertado."
							 );
		}		
		
		function Debug($value) {
			if($value==true || $value==false) $this->_debug = $value;
		}

		function isNumber($field) {
			return (strpos($this->_type[$field],"int")!==false || strpos($this->_type[$field],"double")!==false);
		}
		
		function isDate($field) { 
			return (strpos($this->_type[$field],"time")!==false || strpos($this->_type[$field],"date")!==false);
		}
		
		function isText($field) {
			return (strpos($this->_type[$field],"text")!==false || strpos($this->_type[$field],"char")!==false);
		}
		
		function setPk($value) {
			$this->_pk = $value;
		}
		
		function setId($value)
		{
			$this->_value[$this->_pk] = $value;
		}
		
		function getId()
		{
			return $this->_value[$this->_pk];
		}
		
		function getTypes() {
			return $this->_type;
		}

		function getFields()
		{
			return $this->_field;
		}
						
		function get($field)
		{
			return($this->_value[$field]);
		}
		
		function v($field) // sinonimo de value
		{
			return($this->_value[$field]);
		}

		function set($field, $value)
		{
			if(in_array($field,$this->_field)) { 
				$this->_value[$field] = $value;			
				if(!in_array($field,$this->_newfield)) $this->_newfield[] = $field;
			}
		}
		// get data wit
			
		function Read()
		{
			$result = mysql_query("SELECT * FROM ".$this->_table." where ".$this->_pk." = ".$this->_value[$this->_pk]." LIMIT 1");
			if(mysql_num_rows($result)>0) 
			{
				$row = mysql_fetch_array($result);				
				foreach($this->_field as $k) $this->_value[$k] = $row[$k];
				mysql_free_result($result);
				return true;				
			}
			return false;
		}
		
		function ExistsItem($iditem) // verify if exists a query in database
		{
			$query = "SELECT * FROM ".$this->_table." where ".$this->_pk." =  '$iditem' LIMIT 1;";
			$result = mysql_query($query);
			$ret = (mysql_num_rows($result)>0);
			mysql_free_result($result);
			return $ret;
		}		

		function Save()
		{
			$arraypost=array();			
			foreach($this->_newfield as $k) 
			{						
				$valor = $this->_value[$k];
				if(strpos($this->_type[$k],"char")!==false || strpos($this->_type[$k],"text")!==false) $valor = mysql_escape_string($valor);
				$llevacomillas=true;	
				if($valor=="current_timestamp") $llevacomillas = false;
				if(strtolower($valor)=="null") $llevacomillas = false;				
				if($llevacomillas==true) $valor = "'". $valor."'";
				$arraypost[]="$k=$valor";					
			}				
			$query = "SELECT * FROM ".$this->_table." where ".$this->_pk." = '".$this->_value[$this->_pk]."' LIMIT 1;";
			$result = mysql_query($query);
			$exists = (mysql_num_rows($result)>0);
			mysql_free_result($result);
			if($exists)
			{				
				if(in_array("fecha_modificacion",$this->_field)) $arraypost[]= "fecha_modificacion = current_timestamp";
				$query = "UPDATE ".$this->_table." SET ".implode(", ",$arraypost)." WHERE ".$this->_pk." = ".$this->_value[$this->_pk];
				if($this->_debug) { 
					echo($query."<br>\n");
					$aq = "pk=".$this->_pk.",v=".$this->_value[$this->_pk]." ".addslashes($query);
					mysql_query("insert consultas set consulta = '".$aq."'");
				}
				mysql_query($query);
				$this->result = $this->_message['msg_update'];
			} else {			
				if($this->_value[$this->_pk]>0) {
					$this->result = "El registro no existe en la BD";
					return false;
				}
				if(in_array("stamp",$this->_field)) $arraypost[]= "stamp = current_timestamp";
				if(in_array("ip",$this->_field)) $arraypost[] = "ip='".$_SERVER['REMOTE_ADDR']."'";
				$query = "INSERT INTO ".$this->_table." SET ".implode(', ',$arraypost);
				if($this->_debug) {
					echo("<br>".$query."<br>\n");
					$aq = "pk=".$this->_pk." ".addslashes($query);
					mysql_query("insert consultas set consulta = '".$aq."'");
				}
				mysql_query($query);				
       			$this->_value[$this->_pk] = mysql_insert_id();			
				$this->result = $this->_message['msg_insert'];						

				//$this->result = "INSERT INTO ".$this->_table." SET ".implode(', ',$arraypost);
			}
			$this->_newfield = array();
			return true;
		}		
		
		function Delete()
		{
			mysql_query("DELETE FROM ".$this->_table." WHERE ".$this->_pk."=".$this->_value[$this->_pk].";");			
			$this->result = $this->_message['msg_delete'];
			return true;
		}
}	

?>